package com.sunlands.deskmate.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

/**
 * @author liude
 * @author anjunling@sunlands.com
 */
@Data
@Slf4j
@Component
public class AuthClient {


    @Autowired
    private RestTemplate restTemplate;

    @Value("${security.oauth2.client.client-id}")
    private String clientId;

    @Value("${security.oauth2.client.client-secret}")
    private String clientSecret;

    @Value("${security.oauth2.resource.token-info-uri}")
    private String tokenInfoUrl;

    @Autowired
    private ResourceServerTokenServices resourceServerTokenServices;

    @Autowired
    private ObjectMapper objectMapper;

//    @Value("${custom.auth.secret}")
//    private String secret;
//
//    @Value("${custom.auth.check_token_url}")
//    private String url;

    public AuthClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String token(String token) {
//        String base64Secret = Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes());

//        HttpHeaders headers = new HttpHeaders();
//        headers.add("Authorization", "Basic " + base64Secret);
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        HttpEntity<String> entity = new HttpEntity<>("token=" + token, headers);

//        AuthInfo authInfo = null;
//        try {
//            ResponseEntity<String> response = restTemplate.exchange(tokenInfoUrl + "?token=" + token, HttpMethod.GET, null, String.class);
//            if (response.getStatusCodeValue() == 200) {
//                String responseStr = response.getBody();
//                try {
//                    authInfo = objectMapper.readValue(responseStr, AuthInfo.class);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }catch (Exception e){
//            log.error("token无效 token = {}", token);
//        }

        OAuth2Authentication oAuth2Authentication = resourceServerTokenServices.loadAuthentication(token);
        String username = oAuth2Authentication.getPrincipal().toString();
        return username;
    }

    @Data
    public static class AuthInfo {

        List<String> aud;

        long exp;

        String user_name;

        List<String> authorities;

        String client_id;

        List<String> scope;
    }

}
